package des.wangku.operate.standard.utls;

import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 通过 Preferences 修改注册表 HKEY_LOCAL_MACHINE\Software\JavaSoft\prefs\<br>
 * Preferences pre = Preferences.systemRoot().node("/javaplayer");<br>
 * 换成Preferences pre = Preferences.userRoot().node("/javaplayer");<br>
 * 则相应的 HKEY_LOCAL_MACHINE就成为HKEY_LOCAL_USER。<br>
 * @author Sunjian
 * @version 1.0
 * @since jdk1.8
 */
public class UtilsWindowsRegistry {
	/** 日志 */
	static Logger logger = LoggerFactory.getLogger(UtilsWindowsRegistry.class);
	/**
	 * 判断HKEY_LOCAL_MACHINE\Software\JavaSoft\prefs\下是否有目录
	 * @param term String
	 * @return boolean
	 */
	public static final boolean isTerm(String term) {
		if(term==null ||term.length()==0)return false;
		Preferences pre = Preferences.systemRoot();
		try {
			if (UtilsArrays.isExist(term, pre.childrenNames())) return true;
		} catch (BackingStoreException e) {
			e.printStackTrace();
			return false;
		}
		return false;
	}
	/**
	 * 判断HKEY_LOCAL_MACHINE\Software\JavaSoft\prefs\下是否有键名
	 * @param term String
	 * @param key String
	 * @return boolean
	 */
	public static final boolean isKey(String term,String key) {
		if(term==null ||term.length()==0)return false;
		if(key==null ||key.length()==0)return false;
		if(!isTerm(term))return false;
		Preferences pre = Preferences.systemRoot().node(term);
		try {
			return UtilsArrays.isExist(key, pre.keys());
		} catch (BackingStoreException e) {
			e.printStackTrace();
			return false;
		}
	}
	/**
	 * 判断HKEY_LOCAL_MACHINE\Software\JavaSoft\prefs\下某个键是否有值，如果没有则返回def
	 * @param term String
	 * @param key String
	 * @param def int
	 * @return int
	 */
	public static final int getKeyInt(String term,String key,int def) {
		if(!isKey(term,key)) {
			logger.debug("注册表中没有发现组和键!");
			return def;
		}
		Preferences pre = Preferences.systemRoot().node("/"+term);
		return pre.getInt(key, def);
	}
}
